package core.gui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;

public class Button extends Component{

	protected String name;
	protected boolean isPressed = false;
	protected boolean isOver = false;
	
	public Button(String name){
		this.name = name;
	}
	
	public void setName(String name){
		this.name = name;
	}
	
	public void render(Graphics2D g){
		if(!isVisible){
			return;
		}
		int px = input.getMouseX();
		int py = input.getMouseY();
		boolean contain = boundingRect.contains(px, py);
		//if(!contain){
		if(!isOver){
			renderNormal(g);
		}else if(!isPressed){
			renderContains(g);
		}else if(isPressed){
			renderIsPressed(g);
		}
		drawButton(g);
	}
	
	private BasicStroke stroke = new BasicStroke(1);
	private Font font = new Font("Button", Font.BOLD, 13);
	protected void drawButton(Graphics2D g){
		g.setFont(font);
		int x = (int) boundingRect.getCenterX();
		int y = (int) boundingRect.getCenterY();
		int w = g.getFontMetrics().charsWidth(name.toCharArray(), 0, name.length());
		int h = g.getFontMetrics().getHeight();
		g.setColor(Color.black);
		g.setStroke(stroke);
		g.drawString(name, x-w/2, y+h/4);
	}
	
	Color darkGray = new Color(127, 127, 127);
	Color darkGray2 = new Color(140, 140, 140);
	protected void renderContains(Graphics2D g){
		g.setColor(Color.WHITE);
		g.fill(boundingRect);
		g.setStroke(new BasicStroke(3));
		g.setColor(darkGray);
		g.draw(boundingRect);
	}
	
	protected void renderIsPressed(Graphics2D g){
		g.setColor(Color.WHITE);
		g.fill(boundingRect);
		g.setStroke(new BasicStroke(3));
		g.setColor(Color.lightGray);
		g.draw(boundingRect);
		g.setColor(new Color(1f, 1f, 1f, 0.6f));
		g.setStroke(new BasicStroke(0.8f));
		if(boundingRect instanceof RoundRectangle2D){
			g.drawRoundRect((int)boundingRect.getX()-2, (int)boundingRect.getY()-2, (int)boundingRect.getWidth()+4, (int)boundingRect.getHeight()+4, 10, 10);
		}else{
			g.drawRect((int)boundingRect.getX()-2, (int)boundingRect.getY()-2, (int)boundingRect.getWidth()+4, (int)boundingRect.getHeight()+4);
		}
	}
	
	protected void renderNormal(Graphics2D g){
		g.setColor(Color.WHITE);
		g.fill(boundingRect);
		g.setStroke(new BasicStroke(3));
		g.setColor(Color.black);
		g.draw(boundingRect);
	}
	
	@Override
	public void mouseMoved(MouseEvent e){
		if(boundingRect.contains(e.getX(), e.getY())){
			isOver = true;
			isEntered();
		}else{
			isOver = false;
			isExited();
		}
	}
	
	@Override
	public void mousePressed(MouseEvent e){
		if(!isVisible){
			return;
		}
		if(e.getButton() == MouseEvent.BUTTON1){
			int x = e.getX();
			int y = e.getY();
			if(boundingRect.contains(x, y)){
				isPressed = true;
				isPressed();
			}
		}
	}
	
	@Override
	public void mouseReleased(MouseEvent e){
		if(!isVisible){
			return;
		}
		if(e.getButton() == MouseEvent.BUTTON1){
			int x = e.getX();
			int y = e.getY();
			if(boundingRect.contains(x, y) && isPressed){
				isClicked();
			}
			isPressed = false;
		}
	}
	
	public void isPressed() {
		
	}

	public void isClicked() {
		
	}
	
	public void isEntered() {
		
	}
	
	public void isExited() {
		
	}
	

}
